package pers.hwh.concurrentprogramming.test;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 *
 * @Author: Wenhao Huang
 * @Date: 2025/2/24 17:24
 * @Version: 1.0
 * @Description: 创建线程的三种方式
 */
public class CreateConcurrentWays {
	public static void main(String[] args) throws Exception {
		//				testThread();
		testRunnable();
		//		testCallable();
	}

	//1、thread
	static void testThread() {
		treadA a = new treadA();
		a.start();
	}

	//2、runnable(推荐)
	static void testRunnable() {
		runnableA a = new runnableA();
		Thread thread1 = new Thread(a);
		Thread thread2 = new Thread(a);
		thread1.start();
		thread2.start();
	}

	//3、callable
	static void testCallable() throws Exception {
		callableA a = new callableA();
		FutureTask<String> task = new FutureTask<>(a);
		Thread thread = new Thread(task);
		thread.start();
		System.out.println(task.get());
	}

	static class callableA implements Callable<String> {
		@Override
		public String call() {
			return "run callable";
		}
	}


	static class runnableA implements Runnable {
		@Override
		public void run() {
			System.out.println("run runnable");
		}
	}

	static class treadA extends Thread {
		@Override
		public void run() {
			System.out.println("run thread");
		}
	}
}